<template>
  <div class="toolbar-button" :class="buttonClass">
    <el-tooltip
      :content="tooltip"
      :disabled="!tooltip"
      placement="bottom"
      :open-delay="500"
    >
      <el-button
        :type="buttonType"
        :size="size"
        :disabled="disabled"
        :loading="loading"
        :icon="icon"
        :circle="circle"
        @click="handleClick"
        class="toolbar-btn"
      >
        <slot v-if="!icon"></slot>
        <span v-if="text && !circle" class="button-text">{{ text }}</span>
      </el-button>
    </el-tooltip>
    
    <!-- 下拉菜单 -->
    <el-dropdown
      v-if="dropdown && dropdownItems.length > 0"
      @command="handleDropdownCommand"
      trigger="click"
      class="toolbar-dropdown"
    >
      <el-button
        :type="buttonType"
        :size="size"
        :disabled="disabled"
        class="dropdown-trigger"
      >
        <i class="el-icon-arrow-down"></i>
      </el-button>
      
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          v-for="item in dropdownItems"
          :key="item.value"
          :command="item.value"
          :disabled="item.disabled"
          :divided="item.divided"
        >
          <i v-if="item.icon" :class="item.icon" class="dropdown-icon"></i>
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
export default {
  name: 'ToolbarButton',
  
  props: {
    // 按钮文本
    text: {
      type: String,
      default: ''
    },
    
    // 图标
    icon: {
      type: String,
      default: ''
    },
    
    // 提示文本
    tooltip: {
      type: String,
      default: ''
    },
    
    // 按钮类型
    type: {
      type: String,
      default: 'default'
    },
    
    // 按钮尺寸
    size: {
      type: String,
      default: 'small'
    },
    
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    
    // 是否加载中
    loading: {
      type: Boolean,
      default: false
    },
    
    // 是否激活状态
    active: {
      type: Boolean,
      default: false
    },
    
    // 是否圆形按钮
    circle: {
      type: Boolean,
      default: false
    },
    
    // 是否有下拉菜单
    dropdown: {
      type: Boolean,
      default: false
    },
    
    // 下拉菜单项
    dropdownItems: {
      type: Array,
      default: () => []
    },
    
    // 自定义类名
    customClass: {
      type: String,
      default: ''
    }
  },
  
  computed: {
    buttonClass() {
      return {
        'is-active': this.active,
        'has-dropdown': this.dropdown,
        [this.customClass]: this.customClass
      }
    },
    
    buttonType() {
      if (this.active) {
        return 'primary'
      }
      return this.type
    }
  },
  
  methods: {
    handleClick() {
      if (!this.disabled && !this.loading) {
        this.$emit('click')
      }
    },
    
    handleDropdownCommand(command) {
      this.$emit('dropdown-command', command)
    }
  }
}
</script>

<style lang="scss" scoped>
.toolbar-button {
  display: inline-flex;
  align-items: center;
  
  &.has-dropdown {
    .toolbar-btn {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
      border-right: none;
    }
    
    .dropdown-trigger {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
      padding: 0 8px;
      
      .el-icon-arrow-down {
        font-size: 12px;
      }
    }
  }
  
  .toolbar-btn {
    transition: all 0.2s;
    
    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
    
    .button-text {
      margin-left: 4px;
    }
  }
  
  &.is-active {
    .toolbar-btn {
      box-shadow: 0 2px 4px rgba(64, 158, 255, 0.3);
    }
  }
}

.toolbar-dropdown {
  .dropdown-icon {
    margin-right: 6px;
    font-size: 14px;
  }
}

// 特殊按钮样式
.toolbar-button {
  // 分隔符样式
  &.separator {
    width: 1px;
    height: 20px;
    background: #e4e7ed;
    margin: 0 8px;
    
    .toolbar-btn {
      display: none;
    }
  }
  
  // 标题按钮样式
  &.heading-button {
    .toolbar-btn {
      font-weight: 600;
      
      &.is-h1 { font-size: 18px; }
      &.is-h2 { font-size: 16px; }
      &.is-h3 { font-size: 14px; }
      &.is-h4 { font-size: 13px; }
      &.is-h5 { font-size: 12px; }
      &.is-h6 { font-size: 11px; }
    }
  }
  
  // 颜色按钮样式
  &.color-button {
    .toolbar-btn {
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 2px;
        left: 50%;
        transform: translateX(-50%);
        width: 12px;
        height: 2px;
        background: var(--color, #333);
        border-radius: 1px;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .toolbar-button {
    .toolbar-btn {
      .button-text {
        display: none;
      }
    }
  }
}
</style>